mini.parse();

function CouponRuleHandler() {
};

CouponRuleHandler.instance = new CouponRuleHandler();

CouponRuleHandler.isQueryBtnExpended = false;

//获得全局的规则数据表格对象
CouponRuleHandler.couponRuleGrid = mini.get("couponRuleGrid");

//加载表格数据
CouponRuleHandler.couponRuleGrid.load();

/**
 * 根据用户权限展示功能按钮
 */
CouponRuleHandler.loadToolbar = function() {
	var array = new Array("coupon_rule_add", "coupon_rule_edit", "coupon_rule_del","coupon_rule_refresh");
	var userArr = MiniuiTools.isHasPurview(array);
	
	for (var i = 0; i < userArr.length; i++) {
		mini.get(array[i]).setVisible(userArr[i]);
	}
};
CouponRuleHandler.loadToolbar();

/**
 * 弹出添加规则窗口
 */
CouponRuleHandler.addCouponRule = function() {
	miniWin.afreshNewWin();
	miniWin.setParams(480, 300, "添加规则");
	miniWin.setOption("bodyStyle","padding:0px;");

	miniWin.load("couponRuleForm.jsp",function(){
		var iframe = miniWin.getIFrameEl();
        var data = {action: "new"};
        iframe.contentWindow.CouponRuleFormHandler.passData(data);
	},function(action){
		CouponRuleHandler.couponRuleGrid.reload();
	});
	
	miniWin.show();
};

/**
 * 弹出编辑规则窗口
 */
CouponRuleHandler.editCouponRule = function() {
	var rowData = CouponRuleHandler.couponRuleGrid.getSelected();
	var rowsData = CouponRuleHandler.couponRuleGrid.getSelecteds();
	
	if(rowsData.length == 1){
		miniWin.afreshNewWin();
		miniWin.setParams(480, 300, "编辑规则[<font color='blue'>"	+ rowData.bcrRule + "</font>]");
		miniWin.setOption("bodyStyle","padding:0px;");
	
		miniWin.load("couponRuleForm.jsp",function(){
			var iframe = miniWin.getIFrameEl();
            var data = {action: "edit", bcrId: rowData.bcrId};
            iframe.contentWindow.CouponRuleFormHandler.passData(data);
		},function(action){
			CouponRuleHandler.couponRuleGrid.reload();
		});
		
		miniWin.show();
	}else if(rowsData.length > 1){
		MiniuiTools.miniWarning("系统提示","不能同时编辑多条资源！");
	}else{
		MiniuiTools.miniWarning("系统提示","请选择要编辑的资源！");
	}
	
};

/**
 * 删除规则资源
 */
CouponRuleHandler.delCouponRule = function() {
	var rowsData = CouponRuleHandler.couponRuleGrid.getSelecteds();
	
	if(rowsData.length > 0){
		mini.confirm("确认删除选择的" + rowsData.length + "条记录？" ,"系统提示",
			function(action, value){
				if (action == "ok") {
					var ids = "";
					for ( var i = 0; i < rowsData.length - 1; i++){
						ids += rowsData[i].bcrId+ ",";
					}
					ids += rowsData[rowsData.length - 1].bcrId;
					
					CouponRuleHandler.couponRuleGrid.loading();
                    $.ajax({
                        url: __root + "/delCouponRuleByIds.do",
                        data: {
				        	bcrIds: ids,
				        	lslUser: gbOperateUser,
				    		lslAddress: gbOperateIp
				        },
                        success: function (text) {
                            MiniuiTools.miniInfo("系统提示","操作成功！", function(action){
				        		CouponRuleHandler.couponRuleGrid.reload();
				        	});
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
        					MiniuiTools.miniError("系统提示","操作失败！" + jqXHR.responseText);
                        },
                        complete: function(e){
                        	CouponRuleHandler.couponRuleGrid.unmask();
                        }
                    });	
                } 
			}
		)
	}else{
		 MiniuiTools.miniWarning("系统提示","请选择要删除的资源！");
	}
};

/**
 * 更新规则状态
 */
CouponRuleHandler.updateCouponRuleState = function() {
	if (!MiniuiTools.hasPurview("coupon_rule_edit")) {
		return;
	}
	var rowData = CouponRuleHandler.couponRuleGrid.getSelected();
	var bcrId = rowData.bcrId;	
	CouponRuleHandler.couponRuleGrid.loading();
	
    $.ajax({
        url: __root + "/updateCouponRuleStateById.do",
        data: {
        	bcrId: bcrId,
        	lslUser: gbOperateUser,
    		lslAddress: gbOperateIp
        },
        success: function (text) {
            CouponRuleHandler.couponRuleGrid.reload();
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	MiniuiTools.miniError("系统提示","操作失败！" + jqXHR.responseText);
        },
        complete: function(){
        	CouponRuleHandler.couponRuleGrid.unmask();
        }
    });	
};

/**
 * 执行查询规则的方法
 */
CouponRuleHandler.doQueryCouponRule = function(){
    CouponRuleHandler.couponRuleGrid.load();
};

/**
 * 刷新页面数据及布局
 */
CouponRuleHandler.afresh = function(){
	CouponRuleHandler.doQueryCouponRule();
	mini.get("couponRulePanel").doLayout();
}

/**
 * DrawCell 绘制单元格
 */
CouponRuleHandler.couponRuleGrid.on("drawcell", function(e) {
	 var record = e.record;
     var column = e.column;
     var field = e.field;
     var value = e.value;

     if (column.field == "bcrState") {
        if (e.value == 1) {
            e.cellHtml = "<span class='icon-show-yes' style='cursor: pointer;' onclick='CouponRuleHandler.updateCouponRuleState()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";                    
        } else {
            e.cellHtml = "<span class='icon-show-no' style='cursor: pointer;' onclick='CouponRuleHandler.updateCouponRuleState()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";
        }
    }
});

